<template>
	<view class="page-container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="search-container flex-center linecamp-1" style="margin: 0 16rpx;" @click="jumpBack()">
					<image src="@/static/image/common/search.png" style="width: 32rpx; height: 32rpx"
						class="clear-flex u-m-r-14"></image>
					<view class="u-f-32 clear-flex linecamp-1" style="width: 480rpx;">{{ keyword }}</view>
				</view>
				<view class="clear-flex" @click="jumpBack()">{{ lang_common.cancel }}</view>
			</view>
			
			<view class="flex-center-between" style="padding: 0 12rpx 0 40rpx;">
				<view class="u-f-32 u-lh-44 font-medio">{{ lang_index.searchResult }}</view>
				<image src="@/static/image/common/filter_btn.png" style="height: 88rpx;width: 88rpx;" class="clear-flex"
					@click="filterVisible = true"></image>
			</view>

			<!-- 分割线 -->
			<u-gap height="1" bgColor="rgba(0, 0, 0, 0.07)"></u-gap>
		</view>
		<!-- 列表 -->
		<view>
			<mescroll-body height="calc(100vh - 198rpx - var(--status-bar-height))" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
				<view>
					<view v-for="(item, index) in dataList" :key="index" class="u-p-t-10">
						<orderlist-card :item="item" @change="changeAction({ e: $event, info: item })"></orderlist-card>
					</view>
				</view>
			</mescroll-body>
		</view>
		
		<!-- 筛选弹窗 -->
		<x-popup v-model="filterVisible" mode="bottom" zIndex="999999" @close="filterVisible = false">
			<view class="filter-container">
				<!-- 头部 -->
				<view class="flex-center-between u-m-b-36" style="position: relative;">
					<image src="@/static/image/common/close_grew.png" style="width: 30rpx; height: 28rpx;"
						class="clear-flex" @click="filterVisible = false"></image>
		
					<view class="u-f-36 u-lh-50 common-title font-medio">{{ lang_common.filter }}</view>
		
					<view class="u-f-32 u-lh-44 font-medio" @click="downCallback">{{ lang_common.done }}
					</view>
				</view>
		
				<!-- 分割线 -->
				<u-gap height="3" bgColor="#F9F9F9"></u-gap>
				
				<view class="u-m-t-54 flex-center u-f-30 u-lh-42">
					<view class="u-m-r-28 font-medio color-grew clear-flex">{{ lang_common.status }}</view>
					<view style="width: 100%;" class="clear-flex">
						<scroll-view scroll-x style="width: 100%;" class="clear-flex nowrap u-p-r-60">
							<view v-for="(item, index) in orderStatus" :key="index" :class="['filter-item u-text-center',
							filterForm.orderStatusIndex === index ? 'filter-item-active' : '']" @click="changeIndex('orderStatusIndex', index)">
								<view>{{ lang_order[item.key] }}</view>
								<view v-if="filterForm.orderStatusIndex === index" class="filter-item-icon u-f-20">
									<image src="@/static/image/common/select_on.png" class="filter-item-icon-content">
									</image>
								</view>
							</view>
						</scroll-view>
					</view>
				</view>
			</view>
		</x-popup>
		
		<!-- 按钮操作 -->
		<order-deal v-model="visible" :type="typeKey" :order-number="orderNumber" @change="orderNumber = ''"></order-deal>
		<x-pay-select v-model="selectVisible" :info="info" @change="changeSelect"></x-pay-select>
	</view>
</template>

<script>
	import { orderPage } from '@/api/user/order.js'
	import { payAction } from '@/utils/pay.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				keyword: '',
				
				filterVisible: false,
				filterForm: {
					orderStatusIndex: -1,
				},

				dataList: [],
				
				visible: false,
				typeKey: '',
				orderNumber: '',
				
				info: {},
				selectVisible: false
			}
		},
		computed: {
			orderStatus() {
				// 0待支付，1已支付或待拼团，2待发货或待核销，3待收货或已核销，4待评价5已完成，-1退单，-2支付费用失败,-3,订单已关闭'
				let list = [
					{ key: 'pendingPayment', label: '待付款', value: 0 },
					{ key: 'waitCollage', label: '待拼团', value: 1 },
					{ key: 'delivery', label: '待发货', value: 2 },
					{ key: 'receivedGoods', label: '待收货', value: 3 },
					{ key: 'beEvaluate', label: '待评价', value: 4 },
					{ key: 'completed', label: '已完成', value: 5 }
				]
				return list
			}
		},
		// 获取类型
		onLoad({
			keyword
		}) {
			this.keyword = keyword
		},
		methods: {
			// 选项触发后
			changeIndex(key, index) {
				this.$set(this.filterForm, key, this.filterForm[key] === index ? -1 : index)
			},
			changeAction({ e: { key, orderNumber }, info }) {
				this.info = info
				this.typeKey = key
				this.orderNumber = orderNumber
				if (key === 'toPay') {
					this.selectVisible = true
				} else {
					this.visible = true
				}
			},
			// 点击确定后
			changeSelect(type) {
				console.log(type)
				payAction(type, this.orderNumber).finally(() => {
					this.selectVisible = false
					this.orderNumber = ''
				})
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef']
				let { orderStatusIndex } = this.filterForm
				let queryObj = { keyword: this.keyword }
				if (orderStatusIndex > -1) {
					queryObj.orderStatus = this.orderStatus[orderStatusIndex].value
				}
				orderPage({
					pageNumber: page.num,
					pageSize: page.size,
					...queryObj
				}).then(res => {
					this.filterVisible = false
					node && node.mescroll.endSuccess(res.list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(res.list); //追加新数据
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef']
				node && node.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-container {
		width: 100%;
		padding: 16rpx 30rpx;
		border-radius: 100rpx;
		background-color: #F6F6F6;
	}
	
	.filter {
		&-container {
			padding: 48rpx 36rpx calc(env(safe-area-inset-bottom) + 48rpx);
			border-top-left-radius: 40rpx;
			border-top-right-radius: 40rpx;
			background-color: #FFFFFF;
		}
	
		&-item {
			display: inline-block;
			padding: 10rpx 30rpx;
			margin-right: 20rpx;
			border-radius: 8rpx;
			background-color: rgba(245, 245, 245, 1);
	
			&-active {
				position: relative;
				color: #FC2642;
				background-color: rgba(252, 38, 66, 0.14);
			}
	
			&-icon {
				position: absolute;
				right: 0rpx;
				bottom: 0rpx;
				width: 40rpx;
				height: 32rpx;
				color: #FFFFFF;
				transform: rotate(135deg);
			}
	
			&-icon-content {
				position: absolute;
				top: 0;
				right: 0;
				width: 40rpx;
				height: 32rpx;
				transform: rotate(-135deg);
			}
		}
	}
</style>
